package org.jeecg.modules.nurse_distance.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.nurse_distance.entity.NurseDistance;
import org.jeecg.modules.nurse_distance.service.INurseDistanceService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.nurse_hospital.entity.NurseHospital;
import org.jeecg.modules.nurse_hospital.service.INurseHospitalService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: nurse_distance
 * @Author: jeecg-boot
 * @Date: 2025-01-15
 * @Version: V1.0
 */
@Api(tags = "nurse_distance")
@RestController
@RequestMapping("/nurse_distance/nurseDistance")
@Slf4j
public class NurseDistanceController extends JeecgController<NurseDistance, INurseDistanceService> {
    @Autowired
    private INurseDistanceService nurseDistanceService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private INurseHospitalService nurseHospitalService;

    /**
     * 分页列表查询
     *
     * @param nurseDistance
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "nurse_distance-分页列表查询")
    @ApiOperation(value = "nurse_distance-分页列表查询", notes = "nurse_distance-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent = "/nurse/nurse_distance/NurseDistanceList")
    public Result<IPage<NurseDistance>> queryPageList(NurseDistance nurseDistance,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        String org=req.getHeader("sys_org_code");
        QueryWrapper<NurseDistance> queryWrapper = QueryGenerator.initQueryWrapper(nurseDistance, req.getParameterMap()       );
       if(oConvertUtils.isNotEmpty(org)){
           queryWrapper.eq("sys_org_code",org);
       }
        SysUser sysuser= sysUserService.getLoginUser();
       if(oConvertUtils.isNotEmpty(sysuser) && oConvertUtils.isNotEmpty(sysuser.getOrgCode())){
           queryWrapper.likeRight("sys_org_code",sysuser.getOrgCode());
       }
        Page<NurseDistance> page = new Page<NurseDistance>(pageNo, pageSize);
        IPage<NurseDistance> pageList = nurseDistanceService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param nurseDistance
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "nurse_distance-分页列表查询")
    @ApiOperation(value = "nurse_distance-分页列表查询", notes = "nurse_distance-分页列表查询")
    @GetMapping(value = "/listAll")
    @PermissionData(pageComponent = "/nurse/nurse_distance/NurseDistanceList")
    public Result<IPage<NurseDistance>> queryPageListAll(NurseDistance nurseDistance,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<NurseDistance> queryWrapper = QueryGenerator.initQueryWrapper(nurseDistance, req.getParameterMap()       );
        SysUser sysuser= sysUserService.getLoginUser();
        if(oConvertUtils.isNotEmpty(sysuser) && oConvertUtils.isNotEmpty(sysuser.getOrgCode())){
            queryWrapper.likeRight("sys_org_code",sysuser.getOrgCode());
        }
        Page<NurseDistance> page = new Page<NurseDistance>(pageNo, pageSize);
        IPage<NurseDistance> pageList = nurseDistanceService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    /**


    /**
     *  通过地址Id和机构Id获得交通费列表
     */
    @ApiOperation(value = "nurse_distance-通过地址Id和机构Id查询", notes = "nurse_distance-通过地址Id和机构Id查询")
    @GetMapping(value = "/queryByAddrIdAndHospitalId")
    public Result<List<NurseDistance>> queryByAddrIdAndHospitalId(@RequestParam(name = "AddrId", required = true) String AddrId,
                                                                  @RequestParam(name = "HospitalId", required = true) String HospitalId) {
        List<NurseDistance> NurseDistanceList;
        NurseDistanceList = nurseDistanceService.getByAddrIdAndHospitalId(AddrId,HospitalId);
        if (NurseDistanceList == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(NurseDistanceList);
    }

    /**
     * 添加
     *
     * @param nurseDistance
     * @return
     */
    @AutoLog(value = "nurse_distance-添加")
    @ApiOperation(value = "nurse_distance-添加", notes = "nurse_distance-添加")
    @RequiresPermissions("nurse_distance:nurse_distance:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody NurseDistance nurseDistance) {
        SysUser sysuser= sysUserService.getLoginUser();
        List<NurseHospital> list = nurseHospitalService.list(new LambdaQueryWrapper<NurseHospital>().eq(NurseHospital::getSysOrgCode,sysuser.getOrgCode()));
        if(!list.isEmpty()){
            nurseDistance.setHospital(list.get(0).getName());
        }
        nurseDistanceService.save(nurseDistance);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param nurseDistance
     * @return
     */
    @AutoLog(value = "nurse_distance-编辑")
    @ApiOperation(value = "nurse_distance-编辑", notes = "nurse_distance-编辑")
    @RequiresPermissions("nurse_distance:nurse_distance:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody NurseDistance nurseDistance) {
        nurseDistanceService.updateById(nurseDistance);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "nurse_distance-通过id删除")
    @ApiOperation(value = "nurse_distance-通过id删除", notes = "nurse_distance-通过id删除")
    @RequiresPermissions("nurse_distance:nurse_distance:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        nurseDistanceService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "nurse_distance-批量删除")
    @ApiOperation(value = "nurse_distance-批量删除", notes = "nurse_distance-批量删除")
    @RequiresPermissions("nurse_distance:nurse_distance:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.nurseDistanceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "nurse_distance-通过id查询")
    @ApiOperation(value = "nurse_distance-通过id查询", notes = "nurse_distance-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<NurseDistance> queryById(@RequestParam(name = "id", required = true) String id) {
        NurseDistance nurseDistance = nurseDistanceService.getById(id);
        if (nurseDistance == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(nurseDistance);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param nurseDistance
     */
    @RequiresPermissions("nurse_distance:nurse_distance:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NurseDistance nurseDistance) {
        return super.exportXls(request, nurseDistance, NurseDistance.class, "nurse_distance");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("nurse_distance:nurse_distance:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, NurseDistance.class);
    }

}
